#include <ne.h>
#include <unistd.h>

using namespace ne;
using namespace std;

int main(int argc, char *argv[])
{
    Log::DefaultLog.open();

    MIDIPlayer *pMIDIPlayer = new MIDIPlayer();
    pMIDIPlayer->sendEvent(MIDI_INSTRUMENT, 0, MIDI_SYNTHBASS1, 0);
    pMIDIPlayer->sendEvent(MIDI_CONTROLLER, 0, MIDI_MAIN_VOLUME, 64);

    pMIDIPlayer->sendEvent(MIDI_NOTE_ON, 0, 60, 127);
    usleep(3 * 1000 * 1000);
    pMIDIPlayer->sendEvent(MIDI_NOTE_OFF, 0, 60, 0);

    if (argc < 2)
    {
        Log::DefaultLog.close();
        return 0;
    }

    MIDISequence sequence(0, 0, "MIDI", argv[1], false, 0);
    sequence.load();

    pMIDIPlayer->setSequence(MIDISequencePtr(&sequence));
    pMIDIPlayer->play();
    //pMIDIPlayer->seek(1500);
    while (1)
    {
        printf("Tick:%lf\n", pMIDIPlayer->getPlayedTicks());
        pMIDIPlayer->tick();
        usleep(1024);
        if (!pMIDIPlayer->isPlaying()) break;
    }
    delete pMIDIPlayer;

    Log::DefaultLog.close();
    return 0;
}
